import { request } from '../../utils/request'
const app = getApp()

Page({
  /**
   * 页面的初始数据
   */
  data: {
    curriculumTimes: [
      {
        activityStartTime: '',
        activityEndTime: ''
      }
    ],
    clickIndex: 0,
    fileList: [],
    fileListDetail: [],
    imgUrl: '',
    imgUrlDetail: '',
    name: '',
    price: '',
    location: '',
    introduction: '',
    typeList: [],
    typeCode: '',
    typeName: '',
    otherName: '',
    size: {
      //裁剪尺寸
      width: 160,
      height: 240
    },
    cropSizePercent: 0.6, //裁剪框显示比例
    borderColor: '#fff' //裁剪框边框颜色
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getTypeList()
  },

  toTemplace() {
    wx.navigateTo({
      url:
        '/packagePlan/imgTemplate/imgTemplate?from=' +
        'curriculum'
    })
  },

  toTemplate() {
    wx.navigateTo({
      url:
        '/packagePlan/imgTemplate/imgTemplate?from=' +
        'curriculumDetail&index=' +
        '2'
    })
  },

  getTypeList() {
    request({
      url: '/mini/api/makeActivity/findUserType'
    }).then((resdata) => {
      console.log(resdata)
      if (resdata.code == 200) {
        this.setData({
          typeList: resdata.data
        })
        if (resdata.data.length > 0) {
          let nameList = []
          resdata.data.forEach((item) => {
            nameList.push({
              label: item.name,
              key: item.code
            })
          })
          this.setData({
            typeList: nameList || []
          })
        }
      } else {
        wx.showToast({
          title: resdata.message,
          icon: 'none'
        })
        this.setData({
          typeList: []
        })
      }
    })
  },

  deleteImg() {
    this.setData({
      fileList: [],
      imgUrl: ''
    })
  },

  deleteImgDetail() {
    this.setData({
      fileListDetail: [],
      imgUrlDetail: ''
    })
  },

  selectConfirm(detail) {
    this.setData({
      typeCode: detail.detail.selectItem,
      typeName: detail.detail.selectLabel
    })
  },

  /**
   * 提交表单
   */
  createEvent() {
    if (this.data.name.length <= 0) {
      wx.showToast({
        title: '请输入课程名称',
        icon: 'none'
      })
      return
    }
    if (this.data.price.length <= 0) {
      wx.showToast({
        title: '请输入课程金额',
        icon: 'none'
      })
      return
    }
    if (Number(this.data.price) <= 0) {
      wx.showToast({
        title: '课程金额必须大于0',
        icon: 'none'
      })
      return
    }
    if (this.data.name.length <= 0) {
      wx.showToast({
        title: '请输入课程名称',
        icon: 'none'
      })
      return
    }
    for (
      let i = 0;
      i < this.data.curriculumTimes.length;
      i++
    ) {
      if (
        this.data.curriculumTimes[i]
          .activityStartTime.length <= 0 ||
        this.data.curriculumTimes[i]
          .activityEndTime.length <= 0
      ) {
        wx.showToast({
          title: '请补全课程时间',
          icon: 'none'
        })
        return
      }
    }
    if (this.data.introduction.length <= 0) {
      wx.showToast({
        title: '请选择课程介绍',
        icon: 'none'
      })
      return
    }
    if (this.data.typeName.length <= 0) {
      wx.showToast({
        title: '请选择课程分类',
        icon: 'none'
      })
      return
    }
    if (
      this.data.typeCode == 'OTHER' &&
      this.data.otherName.length <= 0
    ) {
      wx.showToast({
        title: '请输入其他分类名称',
        icon: 'none'
      })
      return
    }
    if (this.data.introduction.length <= 0) {
      wx.showToast({
        title: '请输入课程介绍',
        icon: 'none'
      })
      return
    }
    if (this.data.imgUrl.length <= 0) {
      wx.showToast({
        title: '请选择课程主题图片',
        icon: 'none'
      })
      return
    }
    if (this.data.imgUrlDetail.length <= 0) {
      wx.showToast({
        title: '请选择课程详情图片',
        icon: 'none'
      })
      return
    }
    request({
      url: '/mini/api/mine/courseSave',
      data: {
        title: this.data.name,
        salePrice: Number(this.data.price),
        type: this.data.typeCode,
        otherType: this.data.otherName,
        introduction: this.data.introduction,
        themeImage: this.data.imgUrl,
        detailImage: this.data.imgUrlDetail,
        startTime: this.data.curriculumTimes[0]
          .activityStartTime,
        endTime: this.data.curriculumTimes[0]
          .activityEndTime
      },
      method: 'POST'
    }).then((resdata) => {
      if (resdata.code == 200) {
        wx.showToast({
          title: '创建成功',
          icon: 'none'
        })
        setTimeout(() => {
          wx.navigateBack()
        }, 200)
      } else {
        wx.showToast({
          title: resdata.message,
          icon: 'none'
        })
      }
    })
  },

  /**
   * 选择开始时间确认
   * @param {*} data
   */
  confirmactivityStartTime(data) {
    console.log(data)
    let arr = this.data.curriculumTimes
    arr[this.data.clickIndex].activityStartTime =
      data.detail.selectDate +
      ' ' +
      data.detail.time +
      ':' +
      data.detail.branch +
      ':00'
    this.setData({
      curriculumTimes: arr
    })
  },

  /**
   * 选择结束时间确认
   * @param {*} data
   */
  confirmactivityEndTime(data) {
    let arr = this.data.curriculumTimes
    arr[this.data.clickIndex].activityEndTime =
      data.detail.selectDate +
      ' ' +
      data.detail.time +
      ':' +
      data.detail.branch +
      ':00'
    this.setData({
      curriculumTimes: arr
    })
  },
  clickItem(detail) {
    this.setData({
      clickIndex:
        detail.currentTarget.dataset.index
    })
  },

  uploadImg(image) {
    var _this = this
    const token = wx.getStorageSync('token')
    wx.uploadFile({
      url:
        'https://elf.byxfd.com' +
        '/mini/api/file/upload',
      header: {
        'content-type': 'multipart/form-data',
        Authorization: token || ''
      },
      filePath: image,
      name: 'file',
      success(res) {
        let data = res.data
        if (data) {
          data = JSON.parse(data)
        }
        if (data.code == 200) {
          let fileList = [
            {
              url: data.data.fileUrl,
              thumb: data.data.fileUrl,
              status: 'success',
              isImage: true,
              name: 'img',
              id: '1',
              type: 'image'
            }
          ]
          if (_this.data.from == 'detail') {
            _this.setData({
              fileListDetail: fileList,
              imgUrlDetail: data.data.fileUrl
            })
          } else {
            _this.setData({
              fileList: fileList,
              imgUrl: data.data.fileUrl
            })
          }
        }
      },
      fail(err) {
        console.log(err)
      }
    })
  },

  /**
   * 上传图片
   * @param {*} event
   */
  afterRead(event) {
    const { file } = event.detail
    const image = file.tempFilePath
    this.setData({
      from: 'notdetail',
      size: {
        width: 160,
        height: 90
      }
    })
    wx.navigateTo({
      url:
        '/packageMy/croper/croper?imgSrc=' +
        image +
        '&size=' +
        JSON.stringify(this.data.size)
    })
  },

  /**
   * 上传图片
   * @param {*} event
   */
  afterReadDetail(event) {
    const { file } = event.detail
    const image = file.tempFilePath
    this.setData({
      from: 'detail',
      size: {
        width: 160,
        height: 240
      }
    })
    wx.navigateTo({
      url:
        '/packageMy/croper/croper?imgSrc=' +
        image +
        '&size=' +
        JSON.stringify(this.data.size)
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    if (app.globalData.imgSrc.length > 0) {
      this.uploadImg(app.globalData.imgSrc)
      app.globalData.imgSrc = ''
    }
    if (this.data.imgUrl.length > 0) {
      let fileList = [
        {
          url: this.data.imgUrl,
          thumb: this.data.imgUrl,
          status: 'success',
          isImage: true,
          name: 'img',
          id: '1',
          type: 'image'
        }
      ]
      this.setData({
        fileList: fileList
      })
    }
    if (this.data.imgUrlDetail.length > 0) {
      let fileList = [
        {
          url: this.data.imgUrlDetail,
          thumb: this.data.imgUrlDetail,
          status: 'success',
          isImage: true,
          name: 'img',
          id: '1',
          type: 'image'
        }
      ]
      this.setData({
        fileListDetail: fileList
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {}
})
